﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WebSiteAdminTool.UI.Utils.Interfaces;
using WebSiteAdminTool.UI.Utils.Commands;
using WebSiteAdminTool.UI.Views.Security.Users;
using WebSiteAdminTool.UI.Views.Base;
using WebSiteAdminTool.UI.Views.Security.Roles;
using WebSiteAdminTool.UI.Views.Security.Profiles;
using WebSiteAdminTool.UI.Views.Security.AccessRules;
using WebSiteAdminTool.UI.Views;
using WebSiteAdminTool.UI.Services.Navigation.Messages;
using WebSiteAdminTool.UI.Services.Messaging;

namespace WebSiteAdminTool.UI.Services.Navigation
{
    public class NavigationService : INavigationService
    {
        #region private data

        //private readonly System.Windows.Navigation.NavigationService _navigationService;

        // common navigation destinations
        public ICommand _navigateBackCommand = null;
        public ICommand _navigateHomeCommand = null;
        public ICommand _navigateToAboutCommand = null;
        // security navigation commands
        public ICommand _navigateToUserManagementCommand = null;
        public ICommand _navigateToRoleManagementCommand = null;
        public ICommand _navigateToUserProfileManagementCommand = null;
        public ICommand _navigateToAccessRuleManagementCommand = null;

        #endregion

        #region ctors

        public NavigationService(/*System.Windows.Navigation.NavigationService navigationService*/)
        {
            /*_navigationService = navigationService;*/
            // common navigation destinations
            _navigateBackCommand = new GenericCommand((obj) => this.NavigateBack(), true);
            _navigateHomeCommand = new GenericCommand((obj) => this.Navigate<HomePage>(), true);
            _navigateToAboutCommand = new GenericCommand((obj) => this.Navigate<AboutPage>(), true);
            // security navigation commands
            _navigateToUserManagementCommand = new GenericCommand((obj) => this.Navigate<UserManagementPage>(), true);
            _navigateToRoleManagementCommand = new GenericCommand((obj) => this.Navigate<RoleManagementPage>(), true);
            _navigateToUserProfileManagementCommand = new GenericCommand((obj) => this.Navigate<UserProfileManagementPage>(), true);
            _navigateToAccessRuleManagementCommand = new GenericCommand((obj) => this.Navigate<AccessRuleManagement>(), true);
        }

        #endregion

        #region command accessors

        // common navigation destinations
        public ICommand NavigateBackCommand { get { return _navigateBackCommand; } }
        public ICommand NavigateHomeCommand { get { return _navigateHomeCommand; } }
        public ICommand NavigateToAboutCommand { get { return _navigateToAboutCommand; } }
        // security navigation commands
        public ICommand NavigateToUserManagementCommand { get { return _navigateToUserManagementCommand; } }
        public ICommand NavigateToRoleManagementCommand { get { return _navigateToRoleManagementCommand; } }
        public ICommand NavigateToUserProfileManagementCommand { get { return _navigateToUserProfileManagementCommand; } }
        public ICommand NavigateToAccessRuleManagementCommand { get { return _navigateToAccessRuleManagementCommand; } }

        #endregion

        #region helpers

        //public void Navigate(string url)
        //{
        //    _navigationService.Navigate(new Uri(url, UriKind.RelativeOrAbsolute));
        //}

        //public void Back()
        //{
        //    if (_navigationService.CanGoBack)
        //        _navigationService.GoBack();
        //}

        // helpers
        public Uri GetPageUri<T>() where T : BaseNavigationPage
        {
            string viewName = typeof(T).Name;

            // translate control namespace -> control path
            string controlPath = typeof(T).Namespace.Split(new[] { "Views" }, StringSplitOptions.None)[1];
            controlPath = string.Format("/Views{0}/{1}.xaml", controlPath.Replace(".", "/"), viewName);
            return new Uri(controlPath, UriKind.Relative);
        }

        private void NavigateBack()
        {
            MessageManager.SendMessage(new NavigateBackMessage<bool>(true));
        }

        private void Navigate<T>() where T : BaseNavigationPage
        {
            Uri viewUri = this.GetPageUri<T>();

            MessageManager.SendMessage(new NavigationMessage<Uri>(viewUri));
        }

        #endregion
    }
}
